﻿using NAnt.Core;
using NAnt.Core.Attributes;

namespace Lean.NantTasks.Deployment
{
    /// <summary>
    /// Executes all contained subtasks as another user.
    /// Useful to copy files to a network share.
    /// </summary>
    [TaskName("impersonate")]
    public class Impersonate : TaskContainer
    {
        [TaskAttribute("username"), StringValidator(AllowEmpty = false)]
        public string Username { get; set; }

        [TaskAttribute("domain"), StringValidator(AllowEmpty = false)]
        public string Domain { get; set; }

        [TaskAttribute("password"), StringValidator(AllowEmpty = false)]
        public string Password { get; set; }


        protected override void ExecuteTask()
        {
            Log(Level.Verbose, string.Format("Executing as \"{0}\\{1}\"...", Domain, Username));
            RemoteCredentials.DoAs(Username, Domain, Password, BaseExcecute);
        }

        private void BaseExcecute()
        {
            base.ExecuteTask();
        }
    }
}